package com.solome.util;

import java.util.Random;

/*******************************************************************************************************************************************************************************************************
 * 随机辅助工具
 * 
 * @author soells
 * @version 0.1
 */
public class RandomUtils {
	public static String[] randomChar = { "a", "b", "c", "d", "e", "f", "g",
			"h", "i", "g", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
			"u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7",
			"8", "9", "0" };
	/** 全局随机种子 */
	static long seedDelta = 1;

	/**
	 * 获取随机整数
	 * 
	 * @param nMin
	 *            下限区间
	 * @param nMax
	 *            上限区间
	 * @return 随机数
	 */
	public static int getRandom(int nMin, int nMax) {
		if (nMin >= nMax) {
			return nMin;
		}
		Random tmpRandom = new Random(System.currentTimeMillis()
				+ (++seedDelta));
		int iRet = tmpRandom.nextInt();
		tmpRandom = null;
		if (iRet < 0)
			iRet = -iRet;
		iRet = iRet % (nMax + 1 - nMin) + nMin;
		return iRet;
	}

	/**
	 * 正态分布的随机数
	 * 
	 * @param miu
	 * @param sigma2
	 * @return
	 */
	public static double getRandom_Norm(double miu, double sigma2) {
		double N = 12;
		double x = 0, temp = N;
		do {
			x = 0;
			for (int i = 0; i < N; i++)
				x = x + (Math.random());
			x = (x - temp / 2) / (Math.sqrt(temp / 12));
			x = miu + x * Math.sqrt(sigma2);
		} while (x <= 0); // 在此我把小于0的数排除掉了
		return x;
	}

	/**
	 * 泊松分布的随机数
	 * 
	 * @param Lamda
	 * @return
	 */
	public static double getRandom_P(double Lamda) { // 泊松分布
		double x = 0, b = 1, c = Math.exp(-Lamda), u;
		do {
			u = Math.random();
			b *= u;
			if (b >= c)
				x++;
		} while (b >= c);
		return x;
	}

}
